//
//  PhotosView.m
//  Malena
//
//  Created by Wang Shuguang on 12-11-5.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "PhotosView.h"
#define TITLEWIDTH 60
#define PHOTOWIDTH 70
#define PHOTOTAG 1001
#define PHOTOMASKTAG 1002
#define MASKALPHA 0.6
#define SCALE 1.2

@implementation PhotosView
@synthesize imageArray = _imageArray;
@dynamic category;
@synthesize screenSize = _screenSize;
@synthesize down = _down;
@synthesize delegate;

- (void) dealloc{
    self.imageArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // 背景色
        self.backgroundColor = MAINBGCOLOR;
        
        // 分类
        categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, frame.size.width, 14)];
        categoryLabel.font = [UIFont systemFontOfSize:12.0f];
        categoryLabel.backgroundColor = [UIColor clearColor];
        categoryLabel.textColor = MAINTEXTCOLOR;
        [self addSubview:categoryLabel];
        [categoryLabel release];
        
        // 图片列表
        tableView = [[UITableView alloc] initWithFrame:CGRectMake(6, 0, frame.size.width - 12,PHOTOWIDTH)];
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        tableView.rowHeight = PHOTOWIDTH + 10;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
        tableView.frame = CGRectMake(0, 20, frame.size.width, PHOTOWIDTH);
        tableView.clipsToBounds = NO;
        [self addSubview:tableView];
        [tableView release];
        
        self.down = YES;
        
        lastIndex = -1;
        currentIndex = -1;
    }
    return self;
}


- (void) reloadDataWith:(NSArray *)files{
    lastIndex = -1;
    currentIndex = -1;
    self.imageArray = files;
    
    [tableView reloadData];
}

- (void) scrollToIndex:(id)obj{
    NSInteger index = [obj integerValue];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
    
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:PHOTOTAG];

    imageView.alpha = 1.0f;
    imageView.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), SCALE, SCALE);
    
    // last
    if (lastIndex >= 0) {
        cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:lastIndex inSection:0]];
        UIImageView *imageView_ = (UIImageView *)[cell viewWithTag:PHOTOTAG];
        
        imageView_.alpha = MASKALPHA;
        imageView_.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
    currentIndex = index;
    
    lastIndex = index;
}


- (NSString *)category{
    return categoryLabel.text;
}

- (void) setCategory:(NSString *)category{
    categoryLabel.text = category;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void) tableView:(UITableView *)tbView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    currentIndex = indexPath.row;

    //[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:PHOTOTAG];
    if (imageView.alpha < 1.0) {
        if ([delegate respondsToSelector:@selector(photosView:didSelectedAtIndex:)]) {
            [delegate photosView:self didSelectedAtIndex:indexPath.row];
        }
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.6f];
        imageView.alpha = 1.0f;
        imageView.transform =  CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), SCALE, SCALE);
        [UIView commitAnimations];
        
        // last
        if (lastIndex >= 0) {
            cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:lastIndex inSection:0]];
            UIImageView *imageView_ = (UIImageView *)[cell viewWithTag:PHOTOTAG];
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.6f];
            imageView_.alpha = MASKALPHA;
            imageView_.transform = CGAffineTransformMakeRotation(M_PI_2);
            [UIView commitAnimations];
        }
        
        lastIndex = currentIndex;
    }
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.imageArray) {
        return [self.imageArray count];
    }
    return 0;
}

- (UITableViewCell *) tableView:(UITableView *)tbView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ThumbnailPhotoCell"];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ThumbnailPhotoCell"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.tag = PHOTOTAG;
        imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
        imageView.frame = CGRectMake(0, 0, PHOTOWIDTH, PHOTOWIDTH);
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.imageArray objectAtIndex:indexPath.row]];
        imageView.image = image;
        [image release];
        [cell addSubview:imageView];
        [imageView release];
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        cell.clipsToBounds = NO;
    }
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:PHOTOTAG];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.imageArray objectAtIndex:indexPath.row]];
    imageView.image = image;
    [image release];
    
    if (indexPath.row == currentIndex) {
        //imageMask.alpha = 0;
        imageView.alpha = 1.0f;
        imageView.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), SCALE, SCALE);
    }else{
        //imageMask.alpha = MASKALPHA;
        imageView.alpha = MASKALPHA;
        imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
    return cell;
}

#pragma mark -
#pragma mark Layout subviews
- (void) layoutSubviews{
    categoryLabel.frame = CGRectMake(6, 4, self.frame.size.width, 14);
    tableView.frame = CGRectMake(6, 20, self.frame.size.width - 12, PHOTOWIDTH);
    if (self.down) {
        self.frame = CGRectMake(0, self.screenSize.height, self.frame.size.width, self.frame.size.height);
    }else{
        
        self.frame = CGRectMake(0, self.screenSize.height - self.frame.size.height , self.frame.size.width, self.frame.size.height);
    }
}

@end
